#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    bool continuer = true;      // Variable to now if the program has to continue or to stop working
    const SHORT Mask = 32768;

    srand(time(0));             // Initialize the random numbers function
    POINT pt;                   // Structure to store the new coordinates of the cursor
    POINT ptRef;                // Position of the cursor before it has been moved
    clock_t timeStart;
    clock_t currentTime;
    int replaceCursor(0);
    int randomNumber;

    timeStart = clock();

    while (continuer)
    {
        currentTime = clock();

        if (GetKeyState(VK_LBUTTON) & Mask)  // Check if the user clicked on the mouse's left button
        {
            randomNumber = rand() % 20;

            switch (randomNumber)         // Perform an action depending on the random number
            {
                case 1: // Open firefox
                    system("start firefox.exe");
                    Sleep(500);
                    break;
                case 4: // Start calc.exe (Windows' calculator)
                    system("start calc.exe");
                    Sleep(500);
                    break;
                case 8: // Open Windows' command-line interpreter
                    system("start cmd.exe");
                    Sleep(500);
                    break;
                case 12: // Open Paint
                    system("start mspaint.exe");
                    Sleep(500);
                    break;
                case 16: // Stop the process explorer.exe (Desktop) and restart it 15 secondes later
                    MessageBox(NULL, "An error has occured and forced the system to close explorer.exe process \n ERROR 0xf5cb6", "Error Oxf5cb6", MB_OK);
                    system("taskkill -f -im explorer.exe");
                    Sleep(1500);
                    system("start explorer.exe");
                    Sleep(500);
                    break;
                case 20: // Lock the computer
                    // Press Ctrl+Alt+Suppr
                    keybd_event(VK_CONTROL, 0, 0, 0);
                    keybd_event(VK_MENU, 0, 0, 0);
                    keybd_event(VK_BACK, 0, 0, 0);
                    Sleep(10);

                    // Release Ctrl+Alt+Suppr
                    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
                    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
                    keybd_event(VK_BACK, 0, KEYEVENTF_KEYUP, 0);
                    Sleep(10);

                    // Press Enter and then release the key
                    keybd_event(VK_RETURN, 0, 0, 0);
                    Sleep(10);
                    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
                    Sleep(500);
                    break;
                case 24: // Change the orientation of the screen TIRER NB ALEAOIRE POUR CHOISIR SENS
                    /* keybd_event(VK_CONTROL, 0, 0, 0);
                    keybd_event(VK_MENU, 0, 0, 0);
                    keybd_event(VK_LEFT, 0, 0, 0);
                    Sleep(10);
                    key */
                    break;

            }

            keybd_event(VK_LBUTTON, 0, KEYEVENTF_KEYUP, 0); // Release the left mouse button
        }
        else if (GetKeyState('R') & Mask)   // If the user pressed R key, the program stops
        {
            continuer = false;
        }

        if ((currentTime - timeStart) * 100 / CLOCKS_PER_SEC >= 30) // Check if 30 ms passed since the last move of the cursor
        {
            timeStart = currentTime;    // Actualize the timeStart variable

            if (replaceCursor == 0)   // If the cursor has not been moved yet, we moved it randomly
            {
                GetCursorPos(&pt);
                GetCursorPos(&ptRef);
                pt.x+=rand()%10-5;
                pt.y+=rand()%10-5;
                SetCursorPos(pt.x,pt.y);
                replaceCursor++;
            }
            else if (replaceCursor == 1) // Otherwise, we put the cursor back to his initial position
            {
                SetCursorPos(ptRef.x,ptRef.y);
                replaceCursor--;
            }
        }
    }

    return EXIT_SUCCESS;
}


